In [1]:
options(repr.plot.width=10, repr.plot.height=6.5)   # this command just formats the size of the figures. Adapt to view them nicely
                                                    # in your browser.
In [2]:
# Linear regression

# Cell culture growth at low temperatues

# You found a new cell that you think grows well around freezing (T=0°C)


Temperature = seq(-4,4,by=0.02)    # Set up some temperature points

# Simulate data with error
GrowthRate = 0.2 +0.6*Temperature^2+  rnorm(length(Temperature),mean = 0,sd=1)        # Outcome of your measured data. Ideal slope + random error

plot(Temperature,GrowthRate)
No description has been provided for this image
In [3]:
# hypothesis: Growth rate depends on Temperature^2
#
# model: Y = alpha + beta X^2
# Transform to linear by variable transformation f(X)=Z

T2 = Temperature^2

plot(T2,GrowthRate)
No description has been provided for this image
In [4]:
# Linear regression computed in class:

n=length(T2)

beta = (n*mean(GrowthRate)*mean(T2)- sum(GrowthRate*T2)) / (n*mean(T2)^2-sum(T2^2))
alpha= mean(GrowthRate)-beta*mean(T2)

plot(T2,GrowthRate)
lines(T2,alpha+beta*T2,col="red",lwd=4)

paste('alpha: ', alpha)
paste('beta: ', beta)
'alpha: 0.0935129218424589'
'beta: 0.615786791888906'
No description has been provided for this image
In [5]:
# Now know regression coefficients, replot original variable
plot(Temperature,GrowthRate)
lines(Temperature,alpha+beta*Temperature^2,col="red",lwd=4)
No description has been provided for this image
In [12]:
# How good is the fit: Compare the sum of squares
SSTotal= sum(  (GrowthRate-mean(GrowthRate))^2 )
SSE = sum( (GrowthRate -alpha- beta*Temperature^2 )^2   )
SST = SSTotal-SSE

#Goodness of fit parameter R^2
RSQ=1-SSE/SSTotal

paste('SSTotal: ',SSTotal)
paste('SSE: ',SSE)
paste('SST: ',SST)
paste('R^2: ',RSQ)
'SSTotal: 3680.25549408096'
'SSE: 439.602345641198'
'SST: 3240.65314843976'
'R^2: 0.880551133923115'
In [43]:
# Let us do this in R directly
# First generate a dataframe from your columns, if you dont already have one.

mydata <- data.frame(Temperature,GrowthRate)

quadraticmodel <- lm(GrowthRate ~ I(Temperature^2), data=mydata)
predicted <- predict(quadraticmodel,Temperature=mydata$Temperature)

plot(Temperature,GrowthRate)
lines(Temperature,predicted)
No description has been provided for this image
In [38]:
predicted
201
0.457942057413232
202
0.457991190045255
203
0.458184014874944
204
0.458556878728497
205
0.459133595722249
206
0.459932735518256
207
0.460969792723208
208
0.462258197399863
209
0.46380988034296
210
0.465635625499048
211
0.467745306324305
212
0.470148052917777
213
0.472852375061089
214
0.47586625571874
215
0.479197223940253
216
0.482852412922525
217
0.48683860708696
218
0.491162280836134
219
0.495829630883396
220
0.500846603532687
221
0.506218917931222
222
0.511952086068043
223
0.518051430112167
224
0.524522097552818
225
0.53136907450666
226
0.538597197483206
227
0.546211163843284
228
0.554215541141691
229
0.562614775511062
230
0.571413199216886
231
0.58061503749204
232
0.590224414741807
233
0.600245360196246
234
0.610681813075227
235
0.621537627321936
236
0.6328165759528
237
0.644522355065183
238
0.656658587538718
239
0.669228826461498
240
0.682236558308395
241
0.695685205895452
242
0.709578131131432
243
0.72391863758511
244
0.738709972884847
245
0.753955330965074
246
0.769657854172795
247
0.78582063524576
248
0.80244671917282
249
0.819539104945851
250
0.837100747211732
251
0.855134557832033
252
0.873643407357337
253
0.892630126422458
254
0.912097507068286
255
0.932048303995413
256
0.952485235754305
257
0.973410985876315
258
0.994828203949521
259
1.01673950664298
260
1.03914747868277
261
1.06205467378282
262
1.08546361553341
263
1.10937679824991
264
1.13379668778414
265
1.15872572230058
266
1.18416631301952
267
1.21012084492896
268
1.23659167746721
269
1.26358114517755
270
1.29109155833675
271
1.31912520355872
272
1.34768434437467
273
1.37677122179095
274
1.40638805482587
275
1.43653704102635
276
1.46722035696567
277
1.49844015872311
278
1.53019858234633
279
1.56249774429749
280
1.59533974188375
281
1.6287266536729
282
1.66266053989494
283
1.69714344283008
284
1.73217738718385
285
1.76776438045001
286
1.80390641326158
287
1.84060545973074
288
1.87786347777786
289
1.9156824094504
290
1.95406418123175
291
1.99301070434081
292
2.03252387502244
293
2.0726055748292
294
2.11325767089482
295
2.15448201619962
296
2.19628044982833
297
2.23865479722043
298
2.28160687041349
299
2.32513846827963
300
2.36925137675546
301
2.41394736906565
302
2.45922820594054
303
2.50509563582773
304
2.55155139509819
305
2.59859720824682
306
2.64623478808785
307
2.6944658359451
308
2.74329204183746
309
2.79271508465955
310
2.84273663235797
311
2.89335834210303
312
2.94458186045631
313
2.99640882353409
314
3.04884085716687
315
3.10187957705496
316
3.15552658892046
317
3.20978348865562
318
3.26465186246764
319
3.32013328702031
320
3.37622932957215
321
3.43294154811161
322
3.49027149148904
323
3.54822069954585
324
3.60679070324064
325
3.66598302477273
326
3.72579917770279
327
3.786240667071
328
3.8473089895126
329
3.90900563337099
330
3.97133207880838
331
4.03428979791422
332
4.09788025481124
333
4.16210490575942
334
4.22696519925767
335
4.29246257614363
336
4.35859846969125
337
4.42537430570656
338
4.49279150262144
339
4.56085147158559
340
4.62955561655662
341
4.69890533438848
342
4.76890201491812
343
4.83954704105043
344
4.91084178884168
345
4.98278762758127
346
5.05538591987203
347
5.12863802170889
348
5.20254528255626
349
5.27710904542381
350
5.35233064694102
351
5.42821141743021
352
5.50475268097842
353
5.58195575550784
354
5.65982195284511
355
5.73835257878932
356
5.81754893317882
357
5.89741230995689
358
5.97794399723627
359
6.0591452773625
360
6.14101742697628
361
6.22356171707472
362
6.3067794130715
363
6.39067177485613
364
6.4752400568521
365
6.56048550807415
366
6.64640937218456
367
6.73301288754849
368
6.82029728728847
369
6.90826379933798
370
6.99691364649413
371
7.08624804646957
372
7.1762682119435
373
7.26697535061191
374
7.35837066523704
375
7.45045535369602
376
7.54323060902878
377
7.63669761948526
378
7.73085756857177
379
7.82571163509681
380
7.921260993216
381
8.01750681247655
382
8.11445025786083
383
8.21209248982949
384
8.31043466436377
385
8.40947793300732
386
8.50922344290735
387
8.60967233685509
388
8.71082575332585
389
8.81268482651832
390
8.91525068639343
391
9.01852445871254
392
9.12250726507518
393
9.22720022295624
394
9.33260444574256
395
9.43872104276912
396
9.54555111935462
397
9.65309577683663
398
9.76135611260625
399
9.87033322014225
400
9.98002818904479
401
10.0904421050687
In [ ]: